package com.playball.lottery.frm.yhzx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.JddMainActivity;
import com.playball.lottery.R;
import com.playball.lottery.util.ConvertJsonToList;
import com.tencent.mm.opensdk.modelpay.PayReq;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**微信返回界面
 * Created by aa on 2017/6/20.
 */

public class WXBackActivity extends Activity {
    private TextView text_baek_wx,top_title_play_method;
    private Button btn_bcak_wx,top_title_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        int result=intent.getIntExtra("code",-1);
        setContentView(R.layout.activity_back_wx);
        text_baek_wx= (TextView) findViewById(R.id.text_baek_wx);
        top_title_play_method= (TextView) findViewById(R.id.top_title_play_method);
        btn_bcak_wx= (Button) findViewById(R.id.btn_bcak_wx);
        top_title_back= (Button) findViewById(R.id.top_title_back);
        top_title_back.setVisibility(View.GONE);
        top_title_play_method.setText("支付结果");
        if (result==0){
            text_baek_wx.setText("支付成功");
            getDemand();
        }else if (result==-2){
            text_baek_wx.setText("支付取消");
        }else{
            text_baek_wx.setText("支付出错，请尽快联系客服");
        }
        initdata();
    }

    private void initdata() {
        btn_bcak_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WXBackActivity.this, JddMainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void getDemand() {

        String url;
        if (TCaiRechargeActivity.OUT_TRADE_NO!=null&&"".equals(TCaiRechargeActivity.OUT_TRADE_NO)){
            url= "http://weixin.tcaio2o.com/Recharge/OrderQuery?out_trade_no="+TCaiRechargeActivity.OUT_TRADE_NO;
        }else {
            return;
        }
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                android.util.Log.e("=========1======",e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str=response.body().string();
                if(response.isSuccessful()){
                    android.util.Log.e("========2=======",str);
                    try{
                        JSONTokener jsonParser = new JSONTokener(str);
                        final JSONObject js = (JSONObject) jsonParser.nextValue();
                        WXBackActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try{
                                if ("SUCCESS".equals(js.getString("Code"))) {
                                    text_baek_wx.setText("支付成功");
                                }else {
                                    text_baek_wx.setText("支付如有疑问，请联系店");
                                }
                                }catch (Exception e){

                                }
                            }
                        });

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else {
                    android.util.Log.e("=======3========",str);
                }
            }
        });
    }
}
